package xyz.jcat.biz.admin.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.jcat.biz.admin.domain.dto.RoleAddDto;
import xyz.jcat.biz.admin.domain.dto.RolePermissionBindDto;
import xyz.jcat.biz.admin.service.IRolePermissionService;
import xyz.jcat.biz.admin.service.IRoleService;
import xyz.jcat.common.web.Resp;
import xyz.jcat.web.security.annotation.Menu;
import xyz.jcat.web.security.annotation.RequiresPermission;

import javax.validation.Valid;

@RestController
@RequestMapping("/role")
@Menu(name = "角色管理", value = "role_manage")
public class RoleController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IRolePermissionService rolePermissionService;

    @PostMapping
    @RequiresPermission(name = "添加角色", value = "role:add")
    public Resp addRole(@Valid @RequestBody RoleAddDto roleAddDto) {
        roleService.addRole(roleAddDto);
        return Resp.ok();
    }

    @PostMapping("/permission")
    @RequiresPermission(name = "绑定权限", value = "role:permission:bind")
    public Resp bindRolePermission(@Valid @RequestBody RolePermissionBindDto rolePermissionBindDto) {
        rolePermissionService.bindRolePermission(rolePermissionBindDto);
        return Resp.ok();
    }
}
